package com.dem.javatestwebsocket.service.impl;

import com.dem.javatestwebsocket.service.IMessageRank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Set;
@Service
public class MessageRankService implements IMessageRank {
    private static final String MESSAGE_RANK_KEY = "chat:message_rank";
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    // 增加用户消息计数
    @Override
    public void incrementMessageCount(String userId) {
        redisTemplate.opsForZSet().incrementScore(MESSAGE_RANK_KEY, userId, 1);

    }

    /**
     * 前10排名
     * @return
     */
    @Override
    public Set<ZSetOperations.TypedTuple<String>> getTop10() {
       return redisTemplate.opsForZSet().reverseRangeWithScores(MESSAGE_RANK_KEY,0,9);
    }


}
